package com.ejuornal.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;


public class RandomNumUtil {
    
    //图像
    private ByteArrayInputStream inputStream;
    //验证码
    private String randomCode;
    
    public RandomNumUtil(int width,int height,int size,int fontSize) throws Exception{
        init(width,height,size,fontSize);
    }
    
    private synchronized void init(int width,int height,int size,int fontSize) throws Exception{
        //在内存中创建图像
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //获取图形上下文
        Graphics g = image.getGraphics();
        //生成随机类
        Random random = new Random();
        //设定背景色
        //随机生成颜色
        //g.setColor(getRandColor(160,200));
        //固定淡颜色
        g.setColor(new Color(252 , 252 , 184));
        g.fillRect(0, 0, width, height);
        //设定字体 
        g.setFont(new Font("Times New Roman",Font.BOLD,fontSize));
        //设置边框
        g.setColor(new Color(143, 167, 131));
        g.drawRect(0, 0, width - 1 , height - 1);
        
        //随机产生干扰线
        g.setColor(getRandColor(160,200));
        for(int i = 0;i < 50;i++){
            int x = random.nextInt(width - 1);
            int y = random.nextInt(height - 1);
            int xl = random.nextInt(2) + 1;
            int yl = random.nextInt(4) + 1;
            g.drawLine(x , y , x + xl , y + yl);
        }
        //取随机产生的认证码(size个数字)
        this.randomCode = this.getRandCode(g,size);
        //图想法生效
        g.dispose();
        
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
        ImageIO.write(image,"JPEG",imageOut);
        imageOut.close();
        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
        this.setInputStream(input);
    }
    
    private String getRandCode(Graphics g,int size){
        Random random = new Random();
        char c[] = new char[62];
        
        for(int i = 97, j = 0; i < 123; i++, j++){
            c[j] = (char) i;
        }
        
        for(int o = 65, p = 26; o < 91; o++, p++){
            c[p] = (char) o;
        }
        
        for(int m = 48, n = 52; m < 58; m++, n++){
            c[n] = (char) m;
        }
        
        String sRand = "";
        for(int i = 0; i < size; i++){
            int x = random.nextInt(62);
            String rand = String.valueOf(c[x]);
            sRand += rand;
            
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 13 * i + 6, 16);
        }
        return sRand;
    }
    
    //给定范围获得随机颜色
    private Color getRandColor(int fc,int bc){
        Random random = new Random();
        if(fc > 255){
            fc = 255;
        }
        if(bc > 255){
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r,g,b);
    }
    
    public void setRandomCode(String randomCode){
        this.randomCode = randomCode;
    }
    
    public String getRandomCode() throws Exception{
        return this.randomCode;
    }
    
    public void setInputStream(ByteArrayInputStream inputStream){
        this.inputStream = inputStream;
    }
    
    public ByteArrayInputStream getInputStream(){
        return inputStream;
    }
}
